import 'package:flutter/material.dart';

/// 通用错误视图组件
/// 用于图片加载失败、网络错误等场景
class ErrorViewWidget extends StatelessWidget {
  /// 容器宽度，默认为double.infinity
  final double? width;
  
  /// 容器高度，默认为200
  final double height;
  
  /// 背景色，默认为灰色
  final Color? backgroundColor;
  
  /// 图标，默认为Icons.image
  final IconData icon;
  
  /// 图标颜色，默认为灰色
  final Color? iconColor;
  
  /// 图标大小，默认为高度的30%
  final double? iconSize;
  
  /// 错误提示文字
  final String? message;
  
  /// 文字样式
  final TextStyle? messageStyle;
  
  /// 是否显示重试按钮
  final bool showRetryButton;
  
  /// 重试按钮文字
  final String retryButtonText;
  
  /// 重试按钮样式
  final ButtonStyle? retryButtonStyle;
  
  /// 重试回调
  final VoidCallback? onRetry;
  
  /// 圆角半径
  final double borderRadius;
  
  /// 外边距
  final EdgeInsetsGeometry? margin;
  
  /// 内边距
  final EdgeInsetsGeometry? padding;

  const ErrorViewWidget({
    super.key,
    this.width,
    this.height = 200,
    this.backgroundColor,
    this.icon = Icons.image,
    this.iconColor,
    this.iconSize,
    this.message,
    this.messageStyle,
    this.showRetryButton = false,
    this.retryButtonText = '重试',
    this.retryButtonStyle,
    this.onRetry,
    this.borderRadius = 0,
    this.margin,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width ?? double.infinity,
      height: height,
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        color: backgroundColor ?? Colors.grey[300],
        borderRadius: BorderRadius.circular(borderRadius),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min, // 添加这行，让Column适应内容大小
        children: [
          // 错误图标
          Icon(
            icon,
            color: iconColor ?? (backgroundColor == Colors.transparent ? Colors.grey[600] : Colors.grey),
            size: iconSize ?? (height * 0.3).clamp(16.0, 64.0), 
          ),
          
          // 错误提示文字
          if (message != null && message!.isNotEmpty) ...[
            const SizedBox(height: 4), // 减少间距
            Flexible( // 使用Flexible包装Text，防止溢出
              child: Text(
                message!,
                style: messageStyle ?? TextStyle(
                  color: iconColor ?? (backgroundColor == Colors.transparent ? Colors.grey[600] : Colors.grey),
                  fontSize: 12, // 减小字体大小
                  fontWeight: FontWeight.w500,
                ),
                textAlign: TextAlign.center,
                maxLines: 2, // 限制最大行数
                overflow: TextOverflow.ellipsis, // 超出部分显示省略号
              ),
            ),
          ],
          
          // 重试按钮
          if (showRetryButton) ...[
            const SizedBox(height: 8), // 减少间距
            Flexible( // 使用Flexible包装按钮
              child: ElevatedButton(
                onPressed: onRetry,
                style: retryButtonStyle ?? ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), // 减小按钮内边距
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4),
                  ),
                ),
                child: Text(
                  retryButtonText,
                  style: const TextStyle(fontSize: 12), // 减小按钮文字大小
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }
}

/// 预设的错误视图样式
class ErrorViewPresets {
  /// 图片加载失败样式
  static Widget imageError({
    double? width,
    double height = 200,
    String? message,
    VoidCallback? onRetry,
    double borderRadius = 0,
    Color? backgroundColor,
  }) {
    return ErrorViewWidget(
      width: width,
      height: height,
      icon: Icons.image,
      message: (message != null && message.isNotEmpty) ? message : '图片加载失败',
      showRetryButton: onRetry != null,
      onRetry: onRetry,
      borderRadius: borderRadius,
      backgroundColor: backgroundColor,
    );
  }

  /// 网络错误样式
  static Widget networkError({
    double? width,
    double height = 200,
    String? message,
    VoidCallback? onRetry,
    double borderRadius = 0,
  }) {
    return ErrorViewWidget(
      width: width,
      height: height,
      icon: Icons.wifi_off,
      message: message ?? '网络连接失败',
      showRetryButton: onRetry != null,
      onRetry: onRetry,
      borderRadius: borderRadius,
    );
  }

  /// 数据为空样式
  static Widget emptyData({
    double? width,
    double height = 200,
    String? message,
    VoidCallback? onRetry,
    double borderRadius = 0,
  }) {
    return ErrorViewWidget(
      width: width,
      height: height,
      icon: Icons.inbox,
      message: message ?? '暂无数据',
      showRetryButton: onRetry != null,
      onRetry: onRetry,
      borderRadius: borderRadius,
    );
  }

  /// 加载失败样式
  static Widget loadFailed({
    double? width,
    double height = 200,
    String? message,
    VoidCallback? onRetry,
    double borderRadius = 0,
  }) {
    return ErrorViewWidget(
      width: width,
      height: height,
      icon: Icons.error_outline,
      message: message ?? '加载失败',
      showRetryButton: onRetry != null,
      onRetry: onRetry,
      borderRadius: borderRadius,
    );
  }

  /// 简单错误样式（只有图标）
  static Widget simple({
    double? width,
    double height = 200,
    IconData icon = Icons.image,
    Color? iconColor,
    double? iconSize,
    double borderRadius = 0,
  }) {
    return ErrorViewWidget(
      width: width,
      height: height,
      icon: icon,
      iconColor: iconColor,
      iconSize: iconSize,
      borderRadius: borderRadius,
    );
  }
}
